/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package soft.hb.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import soft.hb.application.HbApplication;
import soft.hb.model.Discharge;
import soft.hb.model.ExhaustCheck;
import soft.hb.model.FileBean;
import soft.hb.model.SurveyRecord;
import soft.hb.model.Task;
import soft.hb.model.Vehicle;
import zuo.biao.library.manager.HttpManager;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.model.Parameter;
import zuo.biao.library.util.JSON;
import zuo.biao.library.util.Log;
import zuo.biao.library.util.MD5Util;
import zuo.biao.library.util.SettingUtil;
import zuo.biao.library.util.StringUtil;

/**HTTP请求工具类
 * @author Lemon
 * @use 添加请求方法xxxMethod >> HttpRequest.xxxMethod(...)
 * @must 所有请求的url、请求方法(GET, POST等)、请求参数(key-value方式，必要key一定要加，没提供的key不要加，value要符合指定范围)
 *       都要符合后端给的接口文档
 */
public class HttpRequest {
	//	private static final String TAG = "HttpRequest";


	/**添加请求参数，value为空时不添加
	 * @param list
	 * @param key
	 * @param value
	 */
	public static void addExistParameter(List<Parameter> list, String key, Object value) {
		if (list == null) {
			list = new ArrayList<Parameter>();
		}
		if (StringUtil.isNotEmpty(key, true) && StringUtil.isNotEmpty(value, true) ) {
			list.add(new Parameter(key, value));
		}
	}



	/**基础URL，这里服务器设置可切换*/
	public static final String URL_BASE = SettingUtil.getCurrentServerAddress();
	public static final String PAGE_NUM = "pageNum";




	//示例代码<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


	//user<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

	public static final String RANGE = "range";
	public static final String JSESSIONID = "JSESSIONID";

	public static final String ID = "id";
	public static final String USER_ID = "userId";
	public static final String CURRENT_USER_ID = "currentUserId";

	public static final String NAME = "name";
	public static final String PHONE = "username";
	public static final String PASSWORD = "password";
	public static final String AUTH_CODE = "authCode";
	public static final String ISMOBILE = "mobileLogin";

	public static final String SEX = "sex";
	public static final int SEX_MAIL = 1;
	public static final int SEX_FEMAIL = 2;
	public static final int SEX_ALL = 3;


	/**翻译，根据有道翻译API文档请求
	 * http://fanyi.youdao.com/openapi?path=data-mode
	 * <br > 本Demo中只有这个是真正可用，其它需要自己根据接口文档新增或修改
	 * @param word
	 * @param requestCode
	 * @param listener
	 */
	public static void translate(String word, final int requestCode, final OnHttpResponseListener listener) {
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, "q", word);
		addExistParameter(paramList, "keyfrom", "ZBLibrary");
		addExistParameter(paramList, "key", 1430082675);
		addExistParameter(paramList, "type", "data");
		addExistParameter(paramList, "doctype", "json");
		addExistParameter(paramList, "version", "1.1");

		HttpManager.getInstance().get(paramList, "http://fanyi.youdao.com/openapi.do", requestCode, listener);
	}



	//account<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

	/**注册
	 * @param phone
	 * @param password
	 * @param listener
	 */
	public static void register(final String phone, final String password,
			final int requestCode, final OnHttpResponseListener listener) {
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, PHONE, phone);
		addExistParameter(paramList, PASSWORD, MD5Util.MD5(password));

		HttpManager.getInstance().post(paramList, URL_BASE + "user/register", requestCode, listener);
	}

	/**登陆
	 * @param phone
	 * @param password
	 * @param listener
	 * username=admin&password=admin&mobileLogin=true
	 */
	public static void login(final String phone, final String password,
			final int requestCode, final OnHttpResponseListener listener) {
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, PHONE, phone);
		addExistParameter(paramList, PASSWORD, password);
		addExistParameter(paramList, ISMOBILE, true);
//		MD5Util.MD5(password)
		HttpManager.getInstance().post(paramList, URL_BASE + "a/login?__ajax", requestCode, listener);
	}

	//account>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



	/**获取用户
	 * @param userId
	 * @param requestCode
	 * @param listener
	 */
	public static void getUser(long userId, final int requestCode, final OnHttpResponseListener listener) {
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, CURRENT_USER_ID, HbApplication.getInstance().getCurrentUserId());
		addExistParameter(paramList, USER_ID, userId);

		HttpManager.getInstance().get(paramList, URL_BASE + "user/information", requestCode, listener);
	}
	public static final int RESULT_GET_USER_SUCCEED = 100;

	public static final int USER_LIST_RANGE_ALL = 0;
	public static final int USER_LIST_RANGE_RECOMMEND = 1;

	/**获取用户列表
	 * @param requestCode
	 * @param listener
	 */
	public static void getUserList(final int requestCode, final OnHttpResponseListener listener) {
		List<Parameter> paramList = new ArrayList<Parameter>();
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		HttpManager.getInstance().get(paramList, URL_BASE + "a/user/list;JSESSIONID="+jsessionId, requestCode, listener);
	}
	public static final int RESULT_GET_USER_LIST_SUCCEED = 110;

	/**获取用户信息
	 * @param requestCode
	 * @param listener
	 */
		public static void getUserInfo(final int requestCode, final OnHttpResponseListener listener) {
		List<Parameter> paramList = new ArrayList<Parameter>();
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		HttpManager.getInstance().get(paramList, URL_BASE + "a/sys/user/infoData;JSESSIONID="+jsessionId, requestCode, listener);
	}


	/**
	 * 现场检查（勘验）笔录
	 * @param
	 */
	public static void submitRecord(final SurveyRecord surveyRecord, final int requestCode, final OnHttpResponseListener listener) {
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, "checkStartTime", surveyRecord.getCheckStartTime());
		addExistParameter(paramList, "checkEndTime", surveyRecord.getCheckEndTime());
		addExistParameter(paramList, "address", surveyRecord.getAddress());
		addExistParameter(paramList, "cardNo.no", surveyRecord.getCardNo());
		addExistParameter(paramList, "noteTaker.id", surveyRecord.getNoteTaker());
		addExistParameter(paramList, "workUnit", surveyRecord.getWorkUnit());
		addExistParameter(paramList, "inspector", surveyRecord.getInspector());
		addExistParameter(paramList, "legalPerson", surveyRecord.getLegalPerson());
		addExistParameter(paramList, "idCard", surveyRecord.getIdCard());
		addExistParameter(paramList, "corporateName", surveyRecord.getCorporateName());
		addExistParameter(paramList, "job", surveyRecord.getJob());
		addExistParameter(paramList, "corporateAddress", surveyRecord.getCorporateAddress());
		addExistParameter(paramList, "zipCode", surveyRecord.getZipCode());
		addExistParameter(paramList, "phone", surveyRecord.getPhone());
		addExistParameter(paramList, "someoneElse", surveyRecord.getSomeoneElse());
		addExistParameter(paramList, "lawEnforcers", surveyRecord.getLawEnforcers());
		addExistParameter(paramList, "lawEnforcersNo", surveyRecord.getLawEnforcersNo());
		addExistParameter(paramList, "personInCharge", surveyRecord.getPersonInCharge());
		addExistParameter(paramList, "carType", surveyRecord.getCarType());
		addExistParameter(paramList, "carModel", surveyRecord.getCarModel());
		addExistParameter(paramList, "licensePlate", surveyRecord.getLicensePlate());
		addExistParameter(paramList, "recordSiteLeader", surveyRecord.getRecordSiteLeader());
		addExistParameter(paramList, "siteLeader", surveyRecord.getSiteLeader());
		addExistParameter(paramList, "siteLeaderDate", surveyRecord.getSiteLeaderDate());
		addExistParameter(paramList, "inspectorPerson.id", surveyRecord.getInspectorPerson());
		addExistParameter(paramList, "inspectorPerson2.id", surveyRecord.getInspectorPerson2());
		addExistParameter(paramList, "inspectorPersonDate", surveyRecord.getInspectorPersonDate());
		addExistParameter(paramList, "recordPerson.id", surveyRecord.getRecordPerson());
		addExistParameter(paramList, "recordPersonDate", surveyRecord.getRecordPersonDate());
		addExistParameter(paramList, "remarks", surveyRecord.getRemarks());

		addExistParameter(paramList, "recordPerson2.id", surveyRecord.getEnforcer2());
		addExistParameter(paramList, "recordPerson3.id", surveyRecord.getEnforcer3());
		addExistParameter(paramList, "submitype", surveyRecord.getSubmitype());

		HttpManager.getInstance().post(paramList, URL_BASE + "a/submitRecord;JSESSIONID="+jsessionId, requestCode, listener);
	}


	/**
	 * 现场检查（勘验）笔录
	 * @param
	 */
	public static void submitDischarge(final Discharge discharge, final int requestCode, final OnHttpResponseListener listener) {
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, "number", discharge.getNumber());
		addExistParameter(paramList, "manufacture", discharge.getManufacture());
		addExistParameter(paramList, "plateNumber", discharge.getPlateNumber());
		addExistParameter(paramList, "model", discharge.getModel());
		addExistParameter(paramList, "subordinateUnit", discharge.getSubordinateUnit());
		addExistParameter(paramList, "registeredAddress", discharge.getRegisteredAddress());
		addExistParameter(paramList, "registeredDate", discharge.getRegisteredDate());
		addExistParameter(paramList, "placeOfDeparture", discharge.getPlaceOfDeparture());
		addExistParameter(paramList, "destination", discharge.getDestination());
		addExistParameter(paramList, "drive", discharge.getDrive());
		addExistParameter(paramList, "position", discharge.getPosition());
		addExistParameter(paramList, "gasStation", discharge.getGasStation());
		addExistParameter(paramList, "inspectionAgencyPosition", discharge.getInspectionAgencyPosition());
		addExistParameter(paramList, "inspectionAgencyName", discharge.getInspectionAgencyName());
		addExistParameter(paramList, "checkTime", discharge.getCheckTime());
		addExistParameter(paramList, "checkAddress", discharge.getCheckAddress());
		addExistParameter(paramList, "checkPeople.id", discharge.getCheckPeople());
		addExistParameter(paramList, "checkisReturnResult", discharge.getCheckisReturnResult());
		addExistParameter(paramList, "district", discharge.getDistrict());
		addExistParameter(paramList, "isReturn", discharge.getIsReturn());

		HttpManager.getInstance().post(paramList, URL_BASE + "a/submitDischarge;JSESSIONID="+jsessionId, requestCode, listener);
	}


	/**
	 * 获取检测配置信息
	 * @param requestCode
	 * @param listener
	 */
	public static void getConfig(final int requestCode, final OnHttpResponseListener listener) {
		List<Parameter> paramList = new ArrayList<Parameter>();
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		HttpManager.getInstance().get(paramList, URL_BASE + "a/getConfig;JSESSIONID="+jsessionId, requestCode, listener);
	}

	/**
	 *
	 * @param
	 */
	public static void uploadSign(final String sign, final int requestCode, final OnHttpResponseListener listener) {
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, "sign", sign);
		HttpManager.getInstance().post(paramList, URL_BASE + "a/uploadSign;JSESSIONID="+jsessionId, requestCode, listener);
	}
	/**
	 *
	 * @param
	 */
	public static void uploadFile(final String files, final int requestCode, final OnHttpResponseListener listener) {
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, "files", files);
		HttpManager.getInstance().post(paramList, URL_BASE + "a/uploadFile;JSESSIONID="+jsessionId, requestCode, listener);
	}


	/**
	 * 现场检查（勘验）笔录
	 * @param
	 */
	public static void submitExhaustCheck(final ExhaustCheck exhaustCheck, final int requestCode, final OnHttpResponseListener listener) {
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, "company", exhaustCheck.getCompany());
		addExistParameter(paramList, "checkDate", exhaustCheck.getCheckDate());
		addExistParameter(paramList, "checkCompany", exhaustCheck.getCheckCompany());
		addExistParameter(paramList, "jurisdiction", exhaustCheck.getJurisdiction());
		addExistParameter(paramList, "plateNumber", exhaustCheck.getPlateNumber());
		addExistParameter(paramList, "model", exhaustCheck.getModel());
		addExistParameter(paramList, "creationDate", exhaustCheck.getCreationDate());
		addExistParameter(paramList, "mileage", exhaustCheck.getMileage());
		addExistParameter(paramList, "purify", exhaustCheck.getPurify());
		addExistParameter(paramList, "smoke1", exhaustCheck.getSmoke1());
		addExistParameter(paramList, "smoke2", exhaustCheck.getSmoke2());
		addExistParameter(paramList, "smoke3", exhaustCheck.getSmoke3());
		addExistParameter(paramList, "smoke4", exhaustCheck.getSmoke4());
		addExistParameter(paramList, "RBAvg", exhaustCheck.getRBAvg());
		addExistParameter(paramList, "KAvg", exhaustCheck.getKAvg());
		addExistParameter(paramList, "checkAddress", exhaustCheck.getCheckAddress());
		addExistParameter(paramList, "instrument", exhaustCheck.getInstrument());
		addExistParameter(paramList, "meter", exhaustCheck.getMeter());
		addExistParameter(paramList, "carType", exhaustCheck.getCarType());
		addExistParameter(paramList, "remarks", exhaustCheck.getRemarks());
		addExistParameter(paramList, "sign", exhaustCheck.getSign());
		addExistParameter(paramList, "photo", exhaustCheck.getPhoto());
		addExistParameter(paramList, "testingPerson.id", exhaustCheck.getTestingPerson());
		addExistParameter(paramList, "reviewer.id", exhaustCheck.getReviewer());
		addExistParameter(paramList, "xinshizheng", exhaustCheck.getXinshizheng());

		HttpManager.getInstance().post(paramList, URL_BASE + "a/submitExhaustCheck;JSESSIONID="+jsessionId, requestCode, listener);
	}



	/**
	 * 获取待办任务
	 * @param requestCode
	 * @param listener
	 */
	public static void pendingTaskList(final int requestCode, final OnHttpResponseListener listener) {
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		List<Parameter> paramList = new ArrayList<Parameter>();
		Log.i("TAG",HbApplication.getInstance().getCurrentUser().getStrId());
		addExistParameter(paramList, "uid", HbApplication.getInstance().getCurrentUser().getStrId());
		HttpManager.getInstance().post(paramList, URL_BASE + "a/pendingTaskList;JSESSIONID="+jsessionId, requestCode, listener);
	}



	/**
	 * 非道路移动器械待办任务项目详情
	 * @param requestCode
	 * @param listener
	 */
	public static void pendingTaskProject(String projectId,final int requestCode, final OnHttpResponseListener listener) {
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		List<Parameter> paramList = new ArrayList<Parameter>();
		Log.i("TAG",HbApplication.getInstance().getCurrentUser().getStrId());
		addExistParameter(paramList, "id", projectId);
		HttpManager.getInstance().post(paramList, URL_BASE + "a/pendingTaskProject;JSESSIONID="+jsessionId, requestCode, listener);
	}



	/**
	 * 现场检查编辑设备
	 * @param
	 */
	public static void modifyVehicle(final Vehicle vehicle, final int requestCode, final OnHttpResponseListener listener) {
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, "id", vehicle.getId());
		addExistParameter(paramList, "voucherNo", vehicle.getVoucherNo());
		addExistParameter(paramList, "KAvg", vehicle.getKAvg());
		addExistParameter(paramList, "detectionTime", vehicle.getDetectionTime());

		HttpManager.getInstance().post(paramList, URL_BASE + "a/updateVehicle;JSESSIONID="+jsessionId, requestCode, listener);
	}




	/**
	 * 现场检查任务提交
	 * @param
	 */
	public static void submitTask(final Task task, final int requestCode, final OnHttpResponseListener listener) {
		String jsessionId = HbApplication.getInstance().getCurrentUser().getJSESSIONID();
		List<Parameter> paramList = new ArrayList<Parameter>();
		addExistParameter(paramList, "id", task.getId());
		HttpManager.getInstance().post(paramList, URL_BASE + "a/submitTask;JSESSIONID="+jsessionId, requestCode, listener);
	}
	//user>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>



	//示例代码>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

}